package com.hualan.zhsq.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;

/**
 * 阿里云OSS工具类
 */
public class OSSUtils {
    // Endpoint
    private static final String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    private static String accessKeyId = "LTAI5tSe13cyPVoFSfkMbDUm";
    private static String accessKeySecret = "32gbyAW2cxFNM07c9Hp98F2V969dOR";
    // 填写Bucket名称
    private static final String bucketName = "sfzy-zx";
    // 填写不包含Bucket名称在内的Object完整路径，例如testfolder/exampleobject.txt。
    private static String objectName = "upload/";

    public static String upload(String fileName, InputStream inputStream) {
        objectName = objectName + fileName;
// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,
                accessKeySecret);
// 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
                objectName, inputStream);
// 创建PutObject请求。
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        String url = "https://sfzy-zx.oss-cn-hangzhou.aliyuncs.com/" + objectName;
        objectName = "upload/";
        return url;
    }
}